﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Exporter;
using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Delivery
{
    /// <summary>
    /// Interaction logic for DeliveryOutQueryUserControl.xaml
    /// </summary>
    public partial class DeliveryOutQueryUserControl : UserControl, IPlugin
    {
        public string Title
        {
            get { return "发货查询"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        public DeliveryOutQueryUserControl()
        {
            InitializeComponent();
        }

        private string GetDeliveryTemplateName(int id)
        {
            var item = ServiceContainer.GetService<DeliveryTemplateService>().GetById(id);
            if (item == null)
            {
                return "未找到模板";
            }
            return item.Name;
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int sellerId = 0;
                long orderId = 0;
                string deliveryCompany = this.cbbDeliveryCompany.Text.Trim();
                string deliveryNumber = this.tbDeliveryNumber.Text.Trim();
                DateTime start = this.dpStart.Value == null ? DateTime.MinValue : this.dpStart.Value.Value;
                DateTime end = this.dpEnd.Value == null ? DateTime.MinValue : this.dpEnd.Value.Value;

                int.TryParse(this.tbSellerId.Text.Trim(), out sellerId);
                long.TryParse(this.tbOrderId.Text.Trim(), out orderId);
                var data = ServiceContainer.GetService<DeliveryOutService>().GetByAll(sellerId, orderId, deliveryCompany, deliveryNumber, this.tbOpeator.Text.Trim(), start, end, 0, 0);
                var group = data.Datas.GroupBy(obj => obj.DeliveryTemplateId);
                var vms = group.Select(new Func<IGrouping<int, ProxyDelivery.Domain.DeliveryOut>, DeliveryOutPageVM>((gs) => new DeliveryOutPageVM(gs.ToArray(), this.GetDeliveryTemplateName(gs.Key)))).ToArray();
                this.tcOrderPages.ItemsSource = vms;
                if (vms.Length > 0)
                {
                    this.tcOrderPages.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var dataPage = (sender as Button).DataContext as DeliveryOutPageVM;
                var data = dataPage.Sources.Select(obj => obj.Source).ToList();
                var dataArray = data.OrderByDescending(obj => obj.Id).ToArray();
                var dataList = new List<DeliveryOut>();

                foreach (var d in dataArray)
                {
                    if (dataList.Find(obj => obj.OrderId == d.OrderId) != null)
                    {
                        continue;
                    }
                    dataList.Add(d);
                }

                Microsoft.Win32.SaveFileDialog ofd = new Microsoft.Win32.SaveFileDialog();
                ofd.FileName = string.Format("51010100073215龙泉成都棒棒娃营销管理有限公司{0}月{1}日的数据.xls", DateTime.Now.Month, DateTime.Now.Day);
                ofd.DefaultExt = "xls";
                bool? ret = ofd.ShowDialog();
                if (ret == false || ret.Value == false)
                {
                    return;
                }
                ExportInfo exportInfo = new ExportInfo { Encoding = Encoding.Default, FilePath = ofd.FileName, FileType = ExportFileType.XLS, SheetName = "sheet1" };
                new YouzhenXiaoBaoExporter().Export(exportInfo, dataList.ToArray());
                MessageBox.Show(ofd.FileName, "已成功导出");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void CheckBox_CheckedChanged(object sender, RoutedEventArgs e)
        {

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            List<string> com = Domain.DeliveryCompanyHelper.GetDeliveryCompany().ToList();
            com.Insert(0, "");
            this.cbbDeliveryCompany.ItemsSource = com;
        }

        private void btnExportMoney_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var dataPage = (sender as Button).DataContext as DeliveryOutPageVM;
                var data = dataPage.Sources.Select(obj => obj.Source).ToList();
                var dataArray = data.OrderByDescending(obj => obj.Id).ToArray();
                var dataList = new List<DeliveryOut>();

                foreach (var d in dataArray)
                {
                    if (dataList.Find(obj => obj.OrderId == d.OrderId) != null)
                    {
                        continue;
                    }
                    dataList.Add(d);
                }
                new DeliveryExportWindow { Outs = dataList.ToArray() }.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
